package com.hmall.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.client.ItemClient;
import com.hmall.common.client.UserClient;
import com.hmall.common.dto.Address;
import com.hmall.common.dto.Item;
import com.hmall.order.common.UserContext;
import com.hmall.order.mapper.OrderDetailMapper;
import com.hmall.order.mapper.OrderMapper;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDetail;
import com.hmall.order.pojo.OrderLogistics;
import com.hmall.order.pojo.RequestParams;
import com.hmall.order.service.IOrderDetailService;
import com.hmall.order.service.IOrderLogisticsService;
import com.hmall.order.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class OrderService extends ServiceImpl<OrderMapper, Order> implements IOrderService {
    @Autowired
    private ItemClient itemClient;
    @Autowired
    private IOrderDetailService detailService;
    @Autowired
    private UserClient userClient;
    @Autowired
    private IOrderLogisticsService logisticsService;
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Override
    @Transactional
    public Long createOrder(RequestParams requestParams) {
        //tb_order相关
        Order order = new Order();
        Long itemId = requestParams.getItemId();
        Item item = itemClient.getById(itemId);
        long totalFee = item.getPrice() * requestParams.getNum();
        order.setTotalFee(totalFee);
        order.setUserId(UserContext.getUserId());
        order.setStatus(1);
        order.setPaymentType(requestParams.getPaymentType());
        this.save(order);
        //tb_order_detail相关
        OrderDetail detail = new OrderDetail();
        detail.setName(item.getName());
        detail.setSpec(item.getSpec());
        detail.setPrice(item.getPrice());
        detail.setNum(requestParams.getNum());
        detail.setItemId(item.getId());
        detail.setImage(item.getImage());
        detail.setOrderId(order.getId());
        detailService.save(detail);

        Address address = userClient.findAddressById(requestParams.getAddressId());
        System.out.println(address);
        OrderLogistics orderLogistics = new OrderLogistics();
        BeanUtils.copyProperties(address,orderLogistics);
        orderLogistics.setOrderId(order.getId());
        logisticsService.save(orderLogistics);
        try {
            itemClient.updateStock(itemId,requestParams.getNum());
        } catch (Exception e) {
            throw new RuntimeException("库存不足！");
        }
        return order.getId();
    }

    @Override
    public void closeTimeoutOrder() {
        List<OrderDetail> orderDetails = orderDetailMapper.queryTimeoutOrderDetail();
        if(orderDetails==null&&orderDetails.size()==0){
            return;
        }
        for (OrderDetail orderDetail : orderDetails) {
            itemClient.updateStock(orderDetail.getItemId(), -orderDetail.getNum());
        }
        List<Long> orderIdList = orderDetails.stream().map(OrderDetail::getOrderId).collect(Collectors.toList());
        LambdaUpdateWrapper<Order> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Order::getStatus, 5);
        updateWrapper.set(Order::getCloseTime, new Date());
        updateWrapper.in(Order::getId, orderIdList);
        this.update(updateWrapper);
        System.out.println(" 执行了定时任务....");
    }
}
